package com.wutuo.wtservice.provider.dao;

import com.wutuo.wtservice.api.request.ListServiceRequest;
import com.wutuo.wtservice.api.request.ServiceRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SqlBuilder;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 服务表 dao sql 层
 *
 * @author lvh
 * @version 1.0
 */
public class ServiceSqlProvider extends BaseServiceSqlProvider {

    public String lisForPage(ListServiceRequest request) {

        BEGIN();
        getSelected();
        FROM("service a ");

        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String list(ServiceRequest request) {

        BEGIN();
        getSelected();
        FROM("service a");

        if (request.getCmInstitutionId() != null) {
            SqlBuilder.WHERE(" a.cm_institution_id = #{cmInstitutionId,jdbcType=VARCHAR}");
        }

        if (request.getCmInstitutionIds() != null) {
            SqlBuilder.WHERE(" a.cm_institution_id in( " + request.getCmInstitutionIds() + ")");
        }

        if (!StringUtils.isEmpty(request.getIds())) {
            SqlBuilder.WHERE(" a.id in (" + request.getIds() + ")");
        }

        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {

        SELECT(
            "id ,name as name,a.cm_institution_id as cmInstitutionId, start_tiem as startTiem,end_time as endTime,type as type,create_by as createBy,create_date as createDate,update_by as updateBy,update_date as updateDate,remarks as remarks,del_flag as delFlag");
    }

}